/**
 * Created by liubin on 2017/2/24.
 */


var WhmjSettingLayer = GameCCSBase.extend({

    Panel_center : null,
    Image_localismSelect : null,
    Text_guangshui : null,
    Text_putong : null,

    ctor : function(){
        this._super();
        this.loadCocostudio("res/ccs/WhmjSetting.json");
        mo.gameHelper.fixIosText(this.ccsNode);

        this.Panel_center = mo.seekWidgetByName(this.ccsNode, "Panel_center");

        this.Image_localismSelect = mo.seekWidgetByName(this.Panel_center, "Image_localismSelect");
        this.Text_guangshui = mo.seekWidgetByName(this.Panel_center, "Text_guangshui");
        this.Text_putong = mo.seekWidgetByName(this.Panel_center, "Text_putong");


        //退出
        mo.seekWidgetByName(this.ccsNode, "Button_ok").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.setDialog(false);
                if (mo.whmjTabel.mPlayerType == 0){
                    mo.whmjTabel.mWhmjReplayLayer.onPlayEnd();
                }else{
                    mo.whmjTabel.mWhmjUILayer.mKwxApplyTableLayer.sendCancelTableAgree();
                }
            }
        }, this);


        mo.seekWidgetByName(this.ccsNode, "Panel_touch").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.setDialog(false);
            }
        }, this);

        //Button_close
        mo.seekWidgetByName(this.Panel_center, "Button_close").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                this.setDialog(false);
            }

        }, this);


        this.Slider_effect = mo.seekWidgetByName(this.ccsNode, "Slider_effect");
        this.Slider_music = mo.seekWidgetByName(this.ccsNode, "Slider_music");
        this.Slider_effect.addEventListener(this.sliderEvent, this);
        this.Slider_music.addEventListener(this.sliderEvent, this);

        //effects
        var Panel_effect = mo.seekWidgetByName(this.ccsNode, "Panel_effect");
        mo.seekWidgetByName(Panel_effect, "Image_close").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.gameUserInfo.setMusicAndEffects(mo.gameUserInfo.mMusicVolume, 0);
            }
        }, this);

        mo.seekWidgetByName(Panel_effect, "Image_open").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.gameUserInfo.setMusicAndEffects(mo.gameUserInfo.mMusicVolume, 100);
            }
        }, this);

        //music
        var Panel_music = mo.seekWidgetByName(this.ccsNode, "Panel_music");
        mo.seekWidgetByName(Panel_music, "Image_close").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.gameUserInfo.setMusicAndEffects(0, mo.gameUserInfo.mEffectsVolume);
            }
        }, this);

        mo.seekWidgetByName(Panel_music, "Image_open").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                mo.gameUserInfo.setMusicAndEffects(100, mo.gameUserInfo.mEffectsVolume);
            }
        }, this);

        // //方言
        // mo.seekWidgetByName(this.Panel_center, "Image_LocalismBg").addTouchEventListener(function (sender, type) {
        //     if (mo.gameHelper.getIsTouchEnd(type)) {
        //         var index = mo.gameUserInfo.mLocalism + 1;
        //         if (index > 1){
        //             index = 0;
        //         }
        //         mo.gameUserInfo.setLocalism(index);
        //     }
        // }, this);

        this.setVisible(false);
    },

    onEnter: function () {
        this._super();

        // //音乐
        this.updateMusicAndEffets(mo.gameUserInfo.mMusicVolume, mo.gameUserInfo.mEffectsVolume);
        mo.eventHelper.add("updateMusicAndEffects", this, function () {
            this.updateMusicAndEffets(mo.gameUserInfo.mMusicVolume, mo.gameUserInfo.mEffectsVolume);
        });

        // //方言
        // this.updateLocalism();
        // mo.eventHelper.add("updateLocalism", this, function(){
        //     this.updateLocalism();
        // });
    },

    onExit : function(){
        mo.eventHelper.remove(this);
        this._super();
    },

    updateMusicAndEffets: function (musicVolume, effectsVolume) {

        mo.seekWidgetByName(this.ccsNode, "Slider_music").setPercent(musicVolume);

        mo.seekWidgetByName(this.ccsNode, "Slider_effect").setPercent(effectsVolume);
    },

    sliderEvent: function (sender, type) {
        switch (type) {
            case ccui.Slider.EVENT_PERCENT_CHANGED:
                if (this.Slider_effect == sender) {
                    var slider = sender;
                    var percent = slider.getPercent();
                    percent = parseInt(percent);
                    mo.soundHelper.setEffectsVolume(percent);
                } else {
                    var slider = sender;
                    var percent = slider.getPercent();
                    percent = parseInt(percent);
                    mo.soundHelper.setMusicVolume(percent);
                }
                break;
            default:
                break;
        }
    },


    //设置本地方言
    updateLocalism : function(){
        if (mo.gameUserInfo.mLocalism == 0){
            this.Image_localismSelect.setPositionX(this.Text_guangshui.getPositionX());
            this.Text_guangshui.setTextColor(cc.color(255, 255, 255));
            this.Text_putong.setTextColor(cc.color(81, 174, 255));
        }else{
            this.Image_localismSelect.setPositionX(this.Text_putong.getPositionX());
            this.Text_putong.setTextColor(cc.color(255, 255, 255));
            this.Text_guangshui.setTextColor(cc.color(81, 174, 255));
        }
    },

    //隐藏或者显示设置窗口
    setDialog : function (isVisible) {
        if (isVisible) {
            this.setVisible(true);
            this.showAction(mo.seekWidgetByName(this.ccsNode, "Panel_center"));
        } else {
            this.closeAction(mo.seekWidgetByName(this.ccsNode, "Panel_center"));
            mo.gameUserInfo.setMusicAndEffects(parseInt(this.Slider_music.getPercent()), parseInt(this.Slider_effect.getPercent()));

        }
    }

});